<?php
/**
 * Created by PhpStorm.
 * User: 84071
 * Date: 2018-08-15
 * Time: 20:56
 */
namespace app\wxapi\controller;

use app\common\controller\Wxapibase;
use app\common\model\Article as cxModel;

class Article extends Wxapibase {

    public function index(){
        $cxmodel = new cxModel();
        $getdata = request()->param();
        $getdata = datatrim($getdata);
        //  获取内容
        $cxcont = $cxmodel->find($getdata['aid']);
        $cxcont->articledb;
        $cxcont['articleconts'] = $cxcont['articledb']['content'];
        unset($cxcont['articledb']);
        unset($parts);
        $cxcont = $cxmodel->readcont($cxcont);
        $cxmodel->save(['hist'=>$cxcont['hist']+1],['aid'=>$cxcont['aid']]);
        $parts = $cxmodel->basi($cxcont);
        //  加载默认模板
        $tempdir = $this->temp;
        $temps = $tempdir.'article.htm';
        if($parts['mid'] != '0'){
            $temps = $tempdir.'article_'.$parts['mid'].'.htm';
        }
        //  获取微信jsapi
        $wxjsapi = $this->get_wxjsapi($this->webdb['www_url'].url('Article/index',array('aid'=>$cxcont['aid'])));
        //  获取评论
        $comment = model('Comment')->where('aid',$cxcont['aid'])->where('status','1')->paginate('30')->each(function ($item,$key){
            $user = model('UserData')->where('uid',$item->uid)->find();
            if(!empty($user)){
                $item->username = $user['username'];
                $item->usericon = $user['uicon'];
            }

        });
        //  获取上一篇和下一篇
        $pageclass['class'] = 'fid';
        $artpage = $this->articlepage($cxcont,$pageclass);
        $webs = array(
            'title' => $cxcont['title'],
            'keywords' => $cxcont['keywords'],
            'description' => $cxcont['description'],
        );
        $this->assign([
            'wxjsapi' => $wxjsapi,
            'contdb' => $cxcont,
            'comment' => $comment,
            'artpage' => $artpage,
            'webs' => $webs,
        ]);
        return view($temps);
    }
    //  获取分页
    public function articlepage($data,$articlass){
        $cxmodel = new cxModel();
        switch ($articlass['class']){
            case 'fid':
                $lastrs = $cxmodel->where('fid',$data['fid'])->where(array('aid' => array('lt',$data['aid']),'status' => 1))->order(array('aid'=>'DESC'))->find();
                $nextrs = $cxmodel->where('fid',$data['fid'])->where(array('aid' => array('gt',$data['aid']),'status' => 1))->order(array('aid'=>'ASC'))->find();
                if (!empty($lastrs) ) {
                    $ptitle = get_word($lastrs['title'],16,false);
                    $articlepage['prev'] = "<a title='{$lastrs['title']}' href='".url('Article/index',array('aid'=>$lastrs['aid']))."' >上一篇<span class='hidden-l'>：{$ptitle}</span></a>";
                } else {
                    $articlepage['prev'] = "<span class='t-gray'>没有了</span>";
                }
                if ( !empty($nextrs) ) {
                    $ntitle = get_word($nextrs['title'],16,false);
                    $articlepage['next'] = "<a title='{$nextrs['title']}' href='".url('Article/index',array('aid'=>$nextrs['aid']))."' >下一篇<span class='hidden-l'>：{$ntitle}</span></a>";
                } else {
                    $articlepage['next'] = "<span class='t-gray'>没有了</span>";
                }
                break;
            case 'fuid':

                break;
        }
        return $articlepage;
    }
}